package com.jiantong.user.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.jiantong.common.constants.JacksonType;
import io.terminus.common.utils.JsonMapper;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

/**
 * @author panxin
 */
@ToString
@EqualsAndHashCode(of = "userId")
@NoArgsConstructor
public class UserProfile implements Serializable {


    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -1216101174153917856L;

    @Getter
    @Setter
    private Long id;                                    //主键

    @Getter
    @Setter
    private Long userId;                                //用户id

    @Getter
    @Setter
    private String realName;                            //真实姓名

    @Getter
    @Setter
    private Integer gender;                             //性别:0男 1女

    @Getter
    @Setter
    private Integer provinceId;                         //省id

    @Getter
    @Setter
    private String province;                            //省

    @Getter
    @Setter
    private Integer cityId;                             //城id

    @Getter
    @Setter
    private String city;                                //城

    @Getter
    @Setter
    private Integer regionId;                           //区id

    @Getter
    @Setter
    private String region;                              //区

    @Getter
    @Setter
    private String street;

    /**
     * 街道ID，可以为空
     */
    @Getter
    @Setter
    private Integer streetId;

    /**
     *
     */
    @Getter
    @Setter
    private String village;

    /**
     *
     */
    @Getter
    @Setter
    private Integer villageId;

    /**
     * 不存存数据库
     */
    @Getter
    private Map<String, String> extra;

    /**
     * 放扩展信息, json存储, 存数据库
     */
    @Getter
    @JsonIgnore
    private String extraJson;


    @Getter
    private Map<String , Map<String , String>> avatarMap;

    @Getter
    private String avatar;


    @Getter
    @Setter
    private String identityCardNumber;                         //身份证


    @Getter
    @Setter
    private Date createdAt;

    @Getter
    @Setter
    private Date updatedAt;

    public UserProfile(Long userId) {
        this.userId = userId;
    }

    public void setExtraJson(String extraJson) throws Exception{
        this.extraJson = extraJson;
        if(Strings.isNullOrEmpty(extraJson)){
            this.extra= Collections.emptyMap();
        } else{
            this.extra = objectMapper.readValue(extraJson, JacksonType.MAP_OF_STRING);
        }
    }

    public void setExtra(Map<String, String> extra) {
        this.extra = extra;
        if(extra ==null ||extra.isEmpty()){
            this.extraJson = null;
        }else{
            try {
                this.extraJson = objectMapper.writeValueAsString(extra);
            } catch (Exception e) {
                //ignore this fuck exception
            }
        }
    }

    public void setAvatarMap(Map<String ,Map<String ,String>> avatarMap){
        this.avatarMap = avatarMap;
        if(avatarMap == null || avatarMap.isEmpty()){
            this.avatar = null;
        }else {
            try {
                this.avatar = objectMapper.writeValueAsString(avatarMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }

    public void setAvatar(String avatar){
        this.avatar = avatar;
        if(Strings.isNullOrEmpty(avatar)){
            this.avatarMap = Maps.newHashMap();
        }else {
            try {
                this.avatarMap = objectMapper.readValue(avatar, new TypeReference<Map<String , Map<String , String>>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

}
